namespace CIS
{
    using CISLib;
    using System;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    public class CATManage : Page
    {
        protected DropDownList AreaDdl;
        protected DataList CATDL;
        protected Image iCATGroupManage;
        protected Label lbArea;
        protected UserPrincipal Principal = null;

        private void AreaDdl_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.CATDLDataBind();
        }

        private void CATDL_CancelCommand(object source, DataListCommandEventArgs e)
        {
            this.CATDL.EditItemIndex = -1;
            this.CATDLDataBind();
        }

        private void CATDL_DeleteCommand(object source, DataListCommandEventArgs e)
        {
            if (!ManageCAT.IsExistCATInOtherTable((Guid) this.CATDL.DataKeys[e.Item.ItemIndex]))
            {
                ManageCAT.RemoveCATViaId((Guid) this.CATDL.DataKeys[e.Item.ItemIndex]);
                this.CATDL.EditItemIndex = -1;
                this.CATDLDataBind();
            }
            else
            {
                base.Response.Write("<script> alert('还有相关信息未被删除！') </script>");
            }
        }

        private void CATDL_EditCommand(object source, DataListCommandEventArgs e)
        {
            this.CATDL.EditItemIndex = e.Item.ItemIndex;
            this.CATDLDataBind();
        }

        private void CATDL_ItemCommand(object source, DataListCommandEventArgs e)
        {
            string str;
            string cmd = e.CommandName;
            if (((str = cmd) != null) && (string.IsInterned(str) == "new"))
            {
                TextBox NameTbx = (TextBox) e.Item.FindControl("txtNewName");
                TextBox DescrTbx = (TextBox) e.Item.FindControl("txtNewDescription");
                if ((NameTbx != null) && (DescrTbx != null))
                {
                    string CATName = NameTbx.Text.Trim();
                    string CATDescr = DescrTbx.Text.Trim();
                    if ((CATName != "") && (CATDescr != ""))
                    {
                        if (ManageCAT.IsExistSameCATName(CATName, new Guid(this.AreaDdl.SelectedValue)))
                        {
                            base.Response.Write("<script> alert('已存在该省') </script>");
                            this.CATDLDataBind();
                        }
                        else
                        {
                            ManageCAT.InsertNewCAT(CATName, CATDescr, new Guid(this.AreaDdl.SelectedValue));
                            this.CATDLDataBind();
                        }
                    }
                }
            }
        }

        private void CATDL_UpdateCommand(object source, DataListCommandEventArgs e)
        {
            TextBox txtName = e.Item.FindControl("txtName") as TextBox;
            TextBox txtDescr = e.Item.FindControl("txtDescription") as TextBox;
            if ((txtName != null) && (txtDescr != null))
            {
                string CATName = txtName.Text.Trim();
                string CATDescr = txtDescr.Text.Trim();
                if (!(CATName == "") && !(CATDescr == ""))
                {
                    ManageCAT.UpdateCAT(CATName, CATDescr, (Guid) this.CATDL.DataKeys[e.Item.ItemIndex]);
                    this.CATDL.EditItemIndex = -1;
                    this.CATDLDataBind();
                }
            }
        }

        private void CATDLDataBind()
        {
            this.CATDL.DataSource = ManageCAT.GetCAT(new Guid(this.AreaDdl.SelectedValue));
            this.CATDL.DataBind();
        }

        protected string GetStrInSetFile(string str)
        {
            string language = this.Session["language"].ToString();
            string path = base.Server.MapPath("~/CIS/LanguageConfig.xml");
            return ManageSystemConfig.GetSystemSetLanguage("CATManage", str, language, path);
        }

        private void InitializeComponent()
        {
            this.AreaDdl.SelectedIndexChanged += new EventHandler(this.AreaDdl_SelectedIndexChanged);
            this.CATDL.ItemCommand += new DataListCommandEventHandler(this.CATDL_ItemCommand);
            this.CATDL.CancelCommand += new DataListCommandEventHandler(this.CATDL_CancelCommand);
            this.CATDL.EditCommand += new DataListCommandEventHandler(this.CATDL_EditCommand);
            this.CATDL.UpdateCommand += new DataListCommandEventHandler(this.CATDL_UpdateCommand);
            this.CATDL.DeleteCommand += new DataListCommandEventHandler(this.CATDL_DeleteCommand);
            base.Load += new EventHandler(this.Page_Load);
        }

        private void InitPage()
        {
            Area[] area = ManageArea.GetAreaAryViaId(this.Principal.SysUser.UserId);
            for (int i = 0; i < area.Length; i++)
            {
                ListItem tmpListItem = new ListItem();
                tmpListItem.Text = area[i].AreaName;
                tmpListItem.Value = area[i].AreaId.ToString();
                this.AreaDdl.Items.Add(tmpListItem);
            }
        }

        protected override void OnInit(EventArgs e)
        {
            this.InitializeComponent();
            base.OnInit(e);
            this.Principal = this.Context.User as UserPrincipal;
        }

        private void Page_Load(object sender, EventArgs e)
        {
            if (this.Principal.SysUser.IsAdmin == 0)
            {
                throw new ApplicationException(string.Format("User: {0} not has privilege to access this page.", this.Principal.SysUser.LoginName));
            }
            if (!base.IsPostBack)
            {
                this.InitPage();
                this.SetFromImage();
                this.CATDLDataBind();
            }
        }

        private void SetFromImage()
        {
            switch (this.Session["language"].ToString())
            {
                case "cn":
                    this.iCATGroupManage.ImageUrl = "images/title_CATGroupManage.jpg";
                    break;

                case "en":
                    this.iCATGroupManage.ImageUrl = "images/title_CATGroupManageEn.jpg";
                    break;
            }
        }
    }
}

